package com.wang.reflectTest;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.wang.reflectTest.Fields.FieldType;

public class CreateSql {
	public static String filter(Object obj){
		StringBuilder stringBuilder = new StringBuilder(" select * from ");
		//获取注解的字节码对象
		Class<Table> table =  Table.class;
		//获取形参对象的字节码对象
		Class<? extends Object> class1 = obj.getClass();
		//获取形参对象中使用的指定注解对象
		Table annotation = class1.getAnnotation(table);
		stringBuilder.append(annotation.tableName()).append(" where 1 = 1 ");
		//引用传递
		getSqlStringBuilder(obj, stringBuilder, class1);
		return stringBuilder.toString();
	}

	private static void getSqlStringBuilder(Object obj, StringBuilder stringBuilder, Class<? extends Object> class1) {
		Class<Fields> fields =  Fields.class;
		Field[] fields2 = class1.getDeclaredFields();//获取所有的字段
		for (Field field : fields2) {
			Fields annotation2 = field.getAnnotation(fields);
			FieldType fieldType = annotation2.fieldType();
			String fieldName = annotation2.fieldName();
			switch(fieldType){
				case EQUAL:
					stringBuilder.append(" and ").append(fieldName).append(" = ");
				try {
					Method method = class1.getMethod("get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1));
					stringBuilder.append("\"").append(method.invoke(obj)).append("\"");
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				}catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
					break;
				case SELECT:;break;
			}
		}
	}
}
